using System;
using System.Drawing.Design;
using System.Security.Permissions;
using System.Windows.Forms;

namespace FB.WinForms.Controls
{
	/// <summary>
	/// Provides a custom user interface for maintaining the Items collection of a HtmlDropDownList.
	/// </summary>
	public class HtmlDropDownListItemsEditor : UITypeEditor
	{
		/// <summary>
		/// Specifies a type editor is a dialog box.
		/// </summary>
		/// <param name="context">An ITypeDescriptorContext that can be used to gain additional context information.</param>
		/// <returns><cref name="UITypeEditorEditStyle.Modal"/> type editor is a dialog box.</returns>
		/// <remarks>Displays an ellipsis (...) button to start a modal, or windowed, dialog box.</remarks>
		[EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted=true)]
		public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
		{
			return UITypeEditorEditStyle.Modal;
		}
	
		/// <summary>
		/// Invoked when the property is edited.
		/// </summary>
		/// <param name="context">An ITypeDescriptorContext that can be used to gain additional context information.</param>
		/// <param name="provider">An IServiceProvider that this editor can use to obtain services.</param>
		/// <param name="value">The <cref name="ComboBox.ObjectCollection"/> to edit.</param>
		/// <returns>The updated <cref name="ComboBox.ObjectCollection"/>.</returns>
		[EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted=true)]
		public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
		{
			object lReturn = value;

			HtmlDropDownList lInstance = (HtmlDropDownList)context.Instance;

			using (HtmlDropDownListItemsEditorUI formEditor = new HtmlDropDownListItemsEditorUI(lInstance.ImageList, lInstance.BaseHref))
			{
				formEditor.Items.Clear();
				foreach (object lObject in (ComboBox.ObjectCollection)value)
				{
					formEditor.Items.Add(lObject); 
				}
				if (formEditor.ShowDialog() == DialogResult.OK)
				{
					lReturn = formEditor.Items;
				}
			}
			return lReturn;
		}
	
		/// <summary>
		/// Indicates the editor does not support painting a representation of the object's value.
		/// </summary>
		/// <param name="context">An ITypeDescriptorContext that can be used to gain additional context information.</param>
		/// <returns>false to specify that PaintValue is not implemented.</returns>
		[EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted=true)]
		public override bool GetPaintValueSupported(System.ComponentModel.ITypeDescriptorContext context)
		{
			return false;
		}
	}
}